/**
 * Monad函子
 */
const fs = require("fs");
const fp = require("lodash/fp");
class IO {
  static of(value) {
    return new IO(() => {
      return value;
    });
  }

  constructor(fn) {
    this._value = fn;
  }

  map(fn) {
    return new IO(fp.flowRight(fn, this._value));
  }

  join(){
    return this._value();
  }

  flatMap(fn){
    return this.map(fn).join();
  }
}

const readFile = (fileName) => {
  return new IO(() => {
    return fs.readFileSync(fileName,"utf-8");
  });
};
const print = (x) => {
  console.log("内部", x);
  return new IO(() => {
    return x;
  });
};

const f = readFile("package.json")
            .flatMap(print);

console.log("f",f)
